package com.example.homework06_intentmultiuse;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8;
    private Uri uri = null;
    Intent intent = new Intent();
    public static final String MYPHONE = "1234567890";
    public static final String MUSICPATH = "/sdcard/Music/半边鱼 - 永不失联的爱.mp3";

    public static final String APKPATH = "res/apk/exp02.apk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //API24以上系统分享支持file:///开头
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectAll();

//        init resource
        btn1 = findViewById(R.id.button1);
        btn2 = findViewById(R.id.button2);
        btn3 = findViewById(R.id.button3);
        btn4 = findViewById(R.id.button4);
        btn5 = findViewById(R.id.button5);
        btn6 = findViewById(R.id.button6);
        btn7 = findViewById(R.id.button7);
        btn8 = findViewById(R.id.button8);
//        onClick
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.button1:
//                Toast.makeText(this, "打开集美大学的网页", Toast.LENGTH_SHORT).show();
                uri = Uri.parse("https://www.jmu.edu.cn/");
                intent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
                break;
            case R.id.button2:
//                Toast.makeText(this, "打开拨号面板", Toast.LENGTH_SHORT).show();
//                Intent.ACTION_DIAL 跳到拨号界面
                Intent dialIntent = new Intent();
                dialIntent.setAction(Intent.ACTION_DIAL);
                startActivity(dialIntent);
                break;
            case R.id.button3:
//                Toast.makeText(this, "直接拨打指定电话号码", Toast.LENGTH_SHORT).show();
//                Intent.ACTION_CALL 直接打电话 （用这个可能会出现安全问题，在真机上不被允许，但是虚拟机可以）
                Intent callIntent = new Intent();
//                callIntent.setAction(Intent.ACTION_CALL);
                callIntent.setAction(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:"+MYPHONE));
                startActivity(callIntent);
                break;
            case R.id.button4:
//                Toast.makeText(this, "打开发短信的界面", Toast.LENGTH_SHORT).show();
                Intent it = new Intent(Intent.ACTION_VIEW);
                it.setType("vnd.android-dir/mms-sms");
                startActivity(it);
                break;
            case R.id.button5:
//                Toast.makeText(this, "给指定的人发短信", Toast.LENGTH_SHORT).show();
//                要注意在安卓手机下，外部的文件一般存放的地方得是在手机文件系统中的特定文件目录下
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("smsto:"+MYPHONE));
                intent.putExtra("sms_body","我是一条指定的信息！");
                startActivity(intent);
                break;
            case R.id.button6:
//                Toast.makeText(this, "播放指定路径下的音乐", Toast.LENGTH_SHORT).show();
//                尚未测试成功
                intent = new Intent(Intent.ACTION_VIEW);
                uri = Uri.parse("file://"+MUSICPATH);
                intent.setDataAndType(uri,"audio/mp3");
                startActivity(intent);
                break;
            case R.id.button7:
//                Toast.makeText(this, "卸载程序", Toast.LENGTH_SHORT).show();
//                尚未测试成功
                intent = new Intent(Intent.ACTION_DELETE);
                uri = Uri.parse("package:com.example.file2");
                intent.setData(uri);
                startActivity(intent);
                break;
            case R.id.button8:
//                Toast.makeText(this, "安装程序", Toast.LENGTH_SHORT).show();
//                尚未测试成功
                intent = new Intent(Intent.ACTION_VIEW);
                uri = Uri.fromFile(new File(APKPATH));
                intent.setDataAndType(uri,
                        "application/vnd.android.package-archive");
                startActivity(intent);
                break;
        }
    }
}